<template>
  <div>
    <!-- <el-button @click="open2"></el-button> -->
    <!-- 带表单的弹出框，药品临期下架 -->
    <div>
      <el-dialog title="药品不合格信息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="药品名字" :label-width="formLabelWidth">
            <el-input
              :disabled="true"
              v-model="form.name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="下架数量" :label-width="formLabelWidth">
            <el-input
              v-model="form.distributionNumber"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="下架原因" :label-width="formLabelWidth">
            <el-input v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="error">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 带表单的弹出框，药品补充库存 -->
    <div>
      <el-dialog title="药品补充" :visible.sync="supplement">
        <el-form :model="supplementForm">
          <el-form-item label="药品名字" :label-width="formLabelWidth">
            <el-input
              :disabled="true"
              v-model="supplementForm.medicineName"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="数量" :label-width="formLabelWidth">
            <el-input
              v-model="supplementForm.cont"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="品牌" :label-width="formLabelWidth">
            <el-input
              v-model="supplementForm.brand"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="supplement = false">取 消</el-button>
          <el-button type="primary" @click="addSupplement">确 定</el-button>
        </div>
      </el-dialog>
    </div>

    <el-table
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%"
    >
      <el-table-column label="保质期" prop="deadline"> </el-table-column>
      <el-table-column label="药品数量" prop="cont"> </el-table-column>
      <el-table-column label="药品名字" prop="name"> </el-table-column>
      <el-table-column label="分类" prop="category"> </el-table-column>
      <el-table-column label="价格" prop="price"> </el-table-column>
      <el-table-column label="品牌" prop="brand"> </el-table-column>
      <el-table-column label="生产日期" prop="dateManufacture">
      </el-table-column>

      <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >库存补充</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >临期处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br /><br />
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[8, 10, 12]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import moment from "moment";
export default {
  mounted() {
    //库存不足预警
    this.$axios
      .get("http://localhost:8080/inventorys/reminderUnderstock",
      )
      .then((src) => {
        if (src.data.data) {
          src.data.data.forEach((element) => {
            this.open1(element.name, element.cont);
          });
        }
      });
    //药品临期预警
    this.$axios
      .get("http://localhost:8080/inventorys/deadlineRemind",)
      .then((src) => {
        if (src.data.data) {
          src.data.data.forEach((element) => {
            this.open2(element.name, element.dateManufacture, element.deadline);
          });
        }
      });
  },
  created() {
    this.$axios
      .get(
        "http://localhost:8080/inventorys/findAllPharmacyStorage/" +
          this.currentPage +
          "/" +
          this.page,
      )
      .then((src) => {
        this.tableData = src.data.data.list;
        this.pageInfo = src.data.data;
      });
  },
  data() {
    return {
      supplement: false,
      supplementForm: {
        medicineName: "",
        cont: "",
        brand: "",
      },
      tableData: [],
      search: "",
      currentPage: 1,
      page: 8,
      pageInfo: {},
      dialogFormVisible: false,
      form: {
        name: "",
        distributionNumber: "",
        description: "药品临期",
        row: {},
      },
      formLabelWidth: "100px",
    };
  },
  methods: {
    //添加需要补充的药品的名字和数量
    addSupplement() {
      this.supplement = false;
      this.$axios
        .post(
          "http://localhost:8080/inventorys/addSupplement",
          this.supplementForm
        )
        .then((src) => {
          if (src.data.code == 200) {
            this.$message({ message: src.data.msg, type: "success" });
          } else {
            this.$message.error(src.data.msg);
          }
        });
    },
    //药品临期处理
    in() {
      this.$axios
        .post("http://localhost:8080/inventorys/issue", {
          medicineId: this.form.row.medicineId,
          description: this.form.description,
          cont: this.form.distributionNumber,
        })
        .then((src) => {
          if (src.data.code == 200) {
            this.$message({ message: src.data.msg, type: "success" });
          } else {
            this.$message.error(src.data.msg);
          }
          this.$axios
            .get(
              "http://localhost:8080/inventorys/findAllPharmacyStorage/" +
                this.currentPage +
                "/" +
                this.page,
            )
            .then((src) => {
              this.tableData = src.data.data.list;
              this.pageInfo = src.data.data;
            });
        });
    },
    error() {
      this.dialogFormVisible = false;
      this.in();
    },
    //通知库存不足的方法
    open1(name, cont) {
      this.timer = window.setTimeout(() => {
        this.$notify({
          title: "库存不足提醒",
          message: name + "库存不足，库存容量剩余" + cont,
        });
      }, 0);
    },
    //通知药物临期的方法
    open2(name, time, deadline) {
      //毫秒转换成天
      let cont =
        deadline -
        (Date.parse(moment(new Date()).format("YYYY/MM/DD")) -
          Date.parse(new Date(time))) /
          (1000 * 60 * 60 * 24);

      // let cont = new Date(time).getTime - moment(new Date()).format("YYYY/MM/DD").getTime

      this.timer = window.setTimeout(() => {
        this.$notify({
          title: "药物临期提醒",
          message: name + "保质期不足三个月，保质期剩余" + cont + "天",
        });
      }, 0);
    },
    handleSizeChange(val) {
      this.page = val;

      this.$axios
        .get(
          "http://localhost:8080/inventorys/findAllPharmacyStorage/" +
            this.currentPage +
            "/" +
            this.page,
        )
        .then((src) => {
          this.tableData = src.data.data.list;
          this.pageInfo = src.data.data;
        });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.$axios
        .get(
          "http://localhost:8080/inventorys/findAllPharmacyStorage/" +
            this.currentPage +
            "/" +
            this.page,
        )
        .then((src) => {
          this.tableData = src.data.data.list;
          this.pageInfo = src.data.data;
        });
    },
    handleEdit(index, row) {
      this.supplement = true;
      this.supplementForm.medicineName = row.name;
      this.supplementForm.brand = row.brand;
    },
    handleDelete(index, row) {
      this.dialogFormVisible = true;
      this.form.name = row.name;
      this.form.row = row;
    },
  },
};
</script>
<style lang="less" scoped></style>
